﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Reflection;
using AMStudio.Script;
using System.Data;

namespace AMStudio.Common
{
    public static class UserInterfaceDB
    {
        private static UserInterfaceTagDAL db = new UserInterfaceTagDAL();

        public static Hashtable UIDBTags;

        public static void Init()
        {
            try
            {
                UIDBTags = db.SelectAllMapper();
            }
            catch (Exception ex)
            {
                throw new Exception("UserInterfaceDB Init Error:" + ex.Message);
            }
        }

        public static void SaveItemChange(string UIName, string controlName, UserInterfaceItem item)
        {
            lock (UIDBTags.SyncRoot)
            {
                string keyName = UIName + "." + controlName;
                if (item == null)
                {
                    if (UIDBTags[keyName] != null)
                    {
                        db.Delete(UIName, controlName);
                        UIDBTags.Remove(keyName);
                    }
                    return;
                }
                if (UIDBTags[keyName] != null)
                {
                    db.Update(item);
                }
                else
                {
                    db.Add(item);
                    UIDBTags.Add(keyName, item);
                }
            }
        }

        public static Dictionary<string, StringValueList<UserInterfaceItem>> GetUIRefreshItems(string UIName)
        {
            Dictionary<string, StringValueList<UserInterfaceItem>> result = new Dictionary<string, StringValueList<UserInterfaceItem>>();
            foreach (DictionaryEntry d in UIDBTags)
            {
                UserInterfaceItem item = d.Value as UserInterfaceItem;
                if (item.UIName == UIName)
                {
                    if (string.IsNullOrEmpty(item.DisplayRefreshGroup))
                    {
                        StringValueList<UserInterfaceItem> valuelist=new StringValueList<UserInterfaceItem>();
                        valuelist.Add(item);
                        if (!string.IsNullOrEmpty(item.DisplayTag))
                        {
                            result.Add(item.ControlName+item.DisplayTag, valuelist);
                        }  
                    }
                    else
                    {
                        if (result[item.DisplayRefreshGroup] == null)
                        {
                            result.Add(item.DisplayRefreshGroup, new StringValueList<UserInterfaceItem>());
                        }
                        if (!string.IsNullOrEmpty(item.DisplayTag))
                        {
                            result[item.DisplayRefreshGroup].Add(item);
                        }                     
                    }
                }           
            }
            return result;
        }
    }
}
